
#include "Repete.h"
#include "Parametre.h"

// Ajoute le code Lobo qui sera analysé

void Repete::addCodeLobo(std::string lobo) {
	m_codeLobo.push_back(lobo);
}

// Fonction qui permet d'analyser le code Lobo et de permettre la traduction en C++

void Repete::analyser() {
	std::stringstream ss;
	for(int i = 0; i < m_codeLobo.size(); ++i){
		ss << m_codeLobo.at(i)+" ";
	}
	std::string s;
	getline(ss, s, ' ');
	if(s.compare("REPETE")==0){
		getline(ss, s, ' ');
		if(TraitementChaine::is_int(s)){
			m_iteration = new Nombre();
			m_iteration->setParametre(s);
		}else{
			m_iteration = new Identifiant();
			m_iteration->setParametre(s);
		}

		getline(ss, s, ' ');
		if(s.compare("DEBUT")==0){
			/*std::cout << ss.str() << std::endl;	*/
			while(!s.compare("FIN_REPETE")==0){
				getline (ss, s, ' ');
				if(s.compare("REPETE")==0){//REPETE
					std::string str = "REPETE";
					int repeteImbrique = 0;
					while(s.compare("FIN_REPETE")!=0 || repeteImbrique!=0){
						if(s.compare("FIN_REPETE")==0){
							--repeteImbrique;
						}
						getline (ss, s, ' ');
						if(s.compare("REPETE")==0){
							++repeteImbrique;
						}

						str+=" "+s;
					}

					str+=" FIN_REPETE";

					m_instructionBases.push_back(new Repete(m_imbrication+1));
					m_instructionBases.at(m_instructionBases.size()-1)->addCodeLobo(str);
					m_instructionBases.at(m_instructionBases.size()-1)->analyser();

				}else if(s.compare("APPEL")==0){ // APPEL FONCTION

					getline (ss, s, ';');
					std::string str = "APPEL "+s+";";

					m_instructionBases.push_back(new AppelFonction());
					m_instructionBases.at(m_instructionBases.size()-1)->addCodeLobo(str);
					m_instructionBases.at(m_instructionBases.size()-1)->analyser();

				}else if(s.compare("DECLARE")==0){ // DECLARE VARIABLE
					getline (ss, s, ';');
					std::string str = "DECLARE "+s+";";

					m_instructionBases.push_back(new DeclareVariable());
					m_instructionBases.at(m_instructionBases.size()-1)->addCodeLobo(str);
					m_instructionBases.at(m_instructionBases.size()-1)->analyser();

				}else if(s.compare("AFFECTE")==0){ // AFFECTE VARIABLE
					getline (ss, s, ';');
					std::string str = "AFFECTE "+s+";";

					m_instructionBases.push_back(new AffecteVariable());
					m_instructionBases.at(m_instructionBases.size()-1)->addCodeLobo(str);
					m_instructionBases.at(m_instructionBases.size()-1)->analyser();

				}else if(s.compare("AFFICHE")==0){ // AFFICHE CHAINE
					getline (ss, s, ';');
					std::string str = "AFFICHE "+s+";";

					m_instructionBases.push_back(new AfficheChaine());
					m_instructionBases.at(m_instructionBases.size()-1)->addCodeLobo(str);
					m_instructionBases.at(m_instructionBases.size()-1)->analyser();

				}
			}

		}else{
			std::cout << "Pas de début ?" << std::endl;
		}


	}else{
		std::cout << "N'est pas une boucle REPETE" << std::endl;
	}
}

// Retourne la fonction Repete dans le langage C++

std::string Repete::toString() const {
	std::ostringstream oss;
	oss << m_imbrication;
	std::string result = oss.str();
	std::string s;
	s+="for (int loop"+result+" = 1; loop"+result+" <="+m_iteration->toString()+"; ++loop"+result+") { \n";
	for(int i = 0 ; i < m_instructionBases.size() ; ++i){
		s+=m_instructionBases.at(i)->toString()+"\n";
	}
	s+="}";
	return s;
}

// Constructeur pour un repete imbriqué

Repete::Repete(int imbrication):m_imbrication(imbrication){

}

// Constructeur utilisé pour un repete qui ne se situe pas dans un autre repete

Repete::Repete():m_imbrication(0){
	// TODO Auto-generated constructor stub

}

// Destructeur qui détruit les variables allouées dynamiquement.

Repete::~Repete(){
	// TODO Auto-generated destructor stub
	delete m_iteration;
	for(int i = 0 ; i < m_instructionBases.size() ; ++i){
		delete m_instructionBases.at(i);
	}
}

